package com.lesso.cyg.sunflower.core;

import javafx.util.Builder;
import lombok.Data;

import java.io.Serializable;

/**
 * ApiResponse
 *
 * @author cyg
 * @since 2025-07-04
 */
@Data
public class ApiResponse<T> implements Serializable {
    private static final long serialVersionUID = 20250703L;

    public static final int SUCCESS = 1;

    public static final int ERROR = 0;

    private int code;

    private String msg;

    private T data;

    private ApiResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ApiResponse<T> ok() {
        return new ApiResponse<>(SUCCESS, null, null);
    }

    public static <T> ApiResponse<T> ok(String msg) {
        return new ApiResponse<>(SUCCESS, msg, null);
    }

    public static <T> ApiResponse<T> ok(String msg, T data) {
        return new ApiResponse<>(SUCCESS, msg, data);
    }

    /**
     * 失败响应
     */
    public static <T> ApiResponse<T> error(String message) {
        return new ApiResponse<>(500, message, null);
    }

    /**
     * 失败响应带错误码
     */
    public static <T> ApiResponse<T> error(Integer code, String message) {
        return new ApiResponse<>(code, message, null);
    }

    /**
     * 参数错误响应
     */
    public static <T> ApiResponse<T> badRequest(String message) {
        return new ApiResponse<>(400, message, null);
    }

    public Builder<T> data(T data) {
        this.data = data;
        return (Builder<T>) this;
    }

    public ApiResponse<T> build() {
        return new ApiResponse<>(code, msg, data);
    }
}
